package com.lx.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lx.common.lang.Result;
import com.lx.entity.CommonUser;
import com.lx.service.ICommonUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 零限Lx
 * @since 2022-10-27
 */
@RestController
@RequestMapping("/common-user")
public class CommonUserController {

    @Autowired
    private ICommonUserService commonUserService;

    @GetMapping("/get/{currentPage}/{pageSize}")
    public Result get(CommonUser commonUser, @PathVariable int currentPage, @PathVariable int pageSize) {
        IPage<CommonUser> page = new Page<>(currentPage, pageSize);
        LambdaQueryWrapper<CommonUser> lqw = new LambdaQueryWrapper<>();
        //条件及分页查询
        lqw.like(StringUtils.isNotBlank(commonUser.getNickname()), CommonUser::getNickname, commonUser.getNickname());
        lqw.like(StringUtils.isNotBlank(commonUser.getMajor()), CommonUser::getMajor, commonUser.getMajor());
        lqw.like(StringUtils.isNotBlank(commonUser.getLoveComic()), CommonUser::getLoveComic, commonUser.getLoveComic());
        IPage<CommonUser> getPage = commonUserService.page(page, lqw);
        return Result.success(getPage);
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable String id) {
        commonUserService.removeById(id);
        return Result.success();
    }

    @PostMapping("/add")
    public Result add(@RequestBody CommonUser commonUser) {
        commonUserService.save(commonUser);
        return Result.success();
    }

    @PutMapping("/edit")
    public Result edit(@RequestBody CommonUser commonUser) {
        commonUserService.updateById(commonUser);
        return Result.success();
    }

    @GetMapping("/getOne/{id}")
    public Result getOne(@PathVariable String id) {
        return Result.success(commonUserService.getById(id));
    }

}
